<template>
	<view class="m30">
		<view class="">
			<image :src="api_host + info.imageShare" mode="widthFix" class="width100"></image>
		</view>
		<u-gap height="30"></u-gap>
		<view class="mb20 flex-au">
			<view class="flex1 mr20">
				<u-button shape="circle" open-type="share" color="#fff" :customStyle="{'color':'#34314F'}" icon="weixin-fill" iconColor="#4CBF00">
					微信
				</u-button>
			</view>
			<view class="flex1 ml20">
				<u-button shape="circle" color="#FFDE6D"
					:customStyle="{'color':'#34314F'}" icon="download" @click="saveImg()">
					保存
				</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getResultbyid
	} from '@/common/api_character.js'
	export default {
		name:'',
		data() {
			return {
				info:{}
			}
		},
		onLoad(option) {
			this.id = option.id || 54
			this.getResultbyid()
		},
		onShareAppMessage(res) {
			return {
				title: '东楚畅通',
				path: '/subpage/caractere/poster?id=' + this.id,
				desc: '能力测评',
				content:'能力测评',
				success(res) {
					uni.showToast({
						title: '分享成功'
					})
				},
				fail(res) {
					uni.showToast({
						title: '分享失败',
						icon: 'none'
					})
				}
			}
		},
		methods: {
			async getResultbyid() {
				uni.showLoading({
					mask: true
				})
				const res = await getResultbyid({
					id: this.id
				})
				if (res.code == 200) {
					this.info = res.data
				}
				uni.hideLoading()
			},
			saveImg() {
				let poster = this.api_host + this.info.imageShare
				let that = this
				uni.downloadFile({
					url: poster, //仅为示例，并非真实的资源
					success: (res) => {
						let tempFilePath = res.tempFilePath
						// //获取授权列表，查看是否授权写入相册权限
						uni.getSetting({
							success(res1) {
								if (!res1.authSetting['scope.writePhotosAlbum']) {
									uni.authorize({
										scope: 'scope.writePhotosAlbum',
										success() {
											that.saveimgtoAlbum(tempFilePath)
										}
									})
								} else {
									that.saveimgtoAlbum(tempFilePath)
								}
							}
						})
					}
				});
			},
			saveimgtoAlbum(imgurl) {
				//图片保存到本地
				let that = this
				uni.showLoading()
				uni.saveImageToPhotosAlbum({
					filePath: imgurl,
					success: function(data) {
						uni.hideLoading()
						uni.showToast({
							title: '保存成功',
							icon: 'success',
							duration: 2000
						})
					},
					fail: function(err) {
						uni.hideLoading()
						if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
							uni.openSetting({
								success(settingdata) {
									if (settingdata.authSetting['scope.writePhotosAlbum']) {
										uni.showToast({
											title: '您已授权成功，请重新保存海报',
											icon: 'success',
											duration: 2000
										})
									} else {
										uni.showToast({
											title: '尚未授权，无法保存海报',
											icon: 'none',
											duration: 2000
										})
									}
								}
							})
						}
					},
					complete(res) {
						console.log(res);
					}
				})
			
			},
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #F1F9F6;
		background-image: linear-gradient(to bottom, #FEA259, #F1F9F6);
	}
</style>
